package com.limh.readers.activity

import android.os.Handler
import android.view.View
import android.view.WindowManager
import android.widget.RelativeLayout
import butterknife.BindView
import butterknife.OnClick
import com.limh.readers.R
import com.limh.readers.comm.Comm
import com.limh.readers.utils.LogUtil
import net.youmi.android.AdManager
import net.youmi.android.nm.sp.SplashViewSettings
import net.youmi.android.nm.sp.SpotListener
import net.youmi.android.nm.sp.SpotManager
import net.youmi.android.nm.sp.SpotRequestListener
import net.youmi.android.nm.cm.ErrorCode


/**
 * Function:
 * author: limh
 * time:2017/7/31
 */
class LauncherActivity : BaseActivity() {

    @BindView(R.id.realive)
    lateinit var realive: RelativeLayout


    val TAG = "LauncherActivity"

    override fun findView() {
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_launcher)
    }

    override fun initViews() {
        AdManager.getInstance(this).init(Comm.YOUMI_APPID, Comm.YOUMI_SERECT, true)
        SpotManager.getInstance(this).requestSpot(object : SpotRequestListener {
            override fun onRequestFailed(errorCode: Int) {
                when (errorCode) {
                    ErrorCode.NON_NETWORK -> LogUtil.d(TAG, "网络异常")
                    ErrorCode.NON_AD -> LogUtil.d(TAG, "暂无视频广告")
                    else -> LogUtil.d(TAG, "请稍后再试")
                }
                handler.sendEmptyMessage(0x124)
            }

            override fun onRequestSuccess() {
                LogUtil.d(TAG, "onRequestSuccess")
                handler.sendEmptyMessage(0x124)
            }
        })

    }

    override fun initDatas() {
    }

    @OnClick(R.id.txt_jump)
    fun OnClick(view: View) {
        when (view.id) {
            R.id.txt_jump -> openActivity(MainActivity::class.java)
        }
    }

    val handler = Handler(Handler.Callback { msg ->
        when (msg.what) {
            0x124 -> setupSplashAd()
        }
        false
    })

    /**
     * 设置开屏广告
     */
    private fun setupSplashAd() {

        val splashViewSettings: SplashViewSettings = SplashViewSettings()
        splashViewSettings.isAutoJumpToTargetWhenShowFailed = true
        splashViewSettings.targetClass = MainActivity::class.java
        splashViewSettings.splashViewContainer = realive
        SpotManager.getInstance(this).showSplash(this, splashViewSettings,
                object : SpotListener {
                    override fun onSpotClicked(p0: Boolean) {
                        LogUtil.d(TAG, "onSpotClicked：" + p0)
                    }

                    override fun onShowSuccess() {
                        LogUtil.d(TAG, "onShowSuccess")
                    }

                    override fun onShowFailed(errorCode: Int) {
                        when (errorCode) {
                            ErrorCode.NON_NETWORK -> LogUtil.d(TAG, "网络异常")
                            ErrorCode.NON_AD -> LogUtil.d(TAG, "暂无开屏广告")
                            ErrorCode.RESOURCE_NOT_READY -> LogUtil.d(TAG, "开屏资源还没准备好")
                            ErrorCode.SHOW_INTERVAL_LIMITED -> LogUtil.d(TAG, "开屏展示间隔限制")
                            ErrorCode.WIDGET_NOT_IN_VISIBILITY_STATE -> LogUtil.d(TAG, "开屏控件处在不可见状态")
                            else -> LogUtil.d(TAG, "errorCode: " + errorCode)
                        }
                    }

                    override fun onSpotClosed() {
                        LogUtil.d(TAG, "onSpotClosed")
                    }
                })
    }

    override fun onDestroy() {
        super.onDestroy()
        SpotManager.getInstance(this).onDestroy()
    }
}
